package app.web.qk.controller;

import app.web.qk.domain.ao.QkDictDeleteAo;
import app.web.qk.service.QkDictService;
import cn.dev33.satoken.annotation.SaIgnore;
import com.gitee.feizns.quickstart.dict.spring.boot.autoconfigure.db.mybatis.flex.pojo.QkDict;
import com.gitee.feizns.quickstart.domain.page.PageRecordsVo;
import com.gitee.feizns.quickstart.domain.vo.Ret;
import com.gitee.feizns.quickstart.log.Log;
import com.gitee.feizns.quickstart.web.domain.query.QueryParamsAo;
import com.gitee.feizns.quickstart.web.restful.Rests;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 通用模块/字典管理
 * @author feizns
 * @since 2025/4/10
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/qk/dict")
public class QkDickController {

    /**
     * 服务
     */
    private final QkDictService service;

    /**
     * 查询字典
     * @param ao 请求参数
     * @return {@link String }
     */
    @SaIgnore
    @Log(type = "通用模块", value = "'获取字典列表'")
    @GetMapping
    public Ret<PageRecordsVo<QkDict>> page(QueryParamsAo ao) {
        return Ret.ok(Rests.page(QkDict.class, ao.page(), ao));
    }

    /**
     * 字典值
     * @param key 钥匙
     * @return {@link Ret }<{@link Object }>
     */
    @Log(type = "通用模块", value = "'获取字典值'")
    @GetMapping("/{key}/value")
    public Ret<Object> value(@PathVariable String key) {
        return Ret.ok(service.value(key).getValue());
    }

    /**
     * 批量获取字典值
     * @param keys 键列表
     * @return {@link Ret }<{@link Map }<{@link String }, {@link Object }>>
     */
    @Log(type = "通用模块", value = "'批量获取字典值'")
    @GetMapping("/values")
    public Ret<Map<String, Object>> values(String[] keys) {
        return Ret.ok(service.values(keys));
    }

    /**
     * 修改字典
     * @param entries 条目
     * @return {@link Ret }<{@link List }<{@link QkDict }>>
     */
    @Transactional
    @Log(type = "通用模块", value = "'修改字典'")
    @PutMapping
    public Ret<Map<String, QkDict>> put(@Validated @RequestBody Map<String, Object> entries) {
        return Ret.ok(service.put(entries));
    }

    /**
     * 删除字典
     * @param ao 请求参数
     * @return {@link Ret }<{@link Void }>
     */
    @Transactional
    @Log(type = "通用模块", value = "'删除字典'")
    @DeleteMapping
    public Ret<Void> delete(@Validated @RequestBody QkDictDeleteAo ao) {
        return Ret.ok(() -> service.delete(ao.getKeys()));
    }

}
